<?php

namespace common\models\member;

use Yii;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%member_privilege}}".
 *
 * @property int $id
 * @property int $type_id 会员类型ID
 * @property string $name 特权名称
 * @property string $slogan 特权标语
 * @property string $description 特权介绍
 * @property string $icon 图标
 * @property string $thumb 缩略图
 * @property string $content 特权详情
 * @property int $listorder 展示排序
 * @property int $status 启用状态
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 * @property int $deleted_at 删除时间
 */
class MemberPrivilege extends \common\db\ActiveRecord {

    use \common\traits\Constants;

    const STATUS_DEFAULT = 0;
    const STATUS_ACTIVE = 10;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%member_privilege}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['type_id', 'listorder', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['name'], 'required'],
            [['type_id', 'listorder'], 'default', 'value' => 0],
            [['content'], 'string'],
            [['name', 'slogan'], 'string', 'max' => 50],
            [['description', 'icon', 'thumb'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'type_id' => '会员类型ID',
            'name' => '特权名称',
            'slogan' => '特权标语',
            'description' => '特权介绍',
            'icon' => '图标',
            'thumb' => '缩略图',
            'content' => '特权详情',
            'listorder' => '展示排序',
            'status' => '启用状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'deleted_at' => '删除时间',
        ];
    }

    /**
     * 关联特权所属会员类型
     */
    public function getType() {
        return $this->hasOne(MemberType::className(), ['id' => 'type_id']);
    }

    /**
     * 会员类型下拉列表
     */
    public function getTypeIdList() {
        return ArrayHelper::map(MemberType::find()->active()->all(), 'id', 'name');
    }

}
